var newCurrent = -1,dailyCurrent = -1;
$(function () {
    signConfig();
    var channel = getQueryString("channel");
    $(".back-wrapper").click(function () {
        location.href = "../index.html?channel="+channel;
    });
    $(".new-sign-btn").click(function () {
        $.ajax({
            type:'get',
            url:BASE_URl + "user/sign/new",
            success:function (data) {
                if(data.success){
                    $(".coin-wrapper p").html(data.data.coin);
                    $(".gift-wrapper p").html(data.data.gift);
                    $(".award-detail-wrapper li").eq(newCurrent).addClass("active").children("p").addClass("active");
                    $(".new-sign-btn").hide().siblings(".new-btn").show();
                    Toast("领取成功");
                }
            }
        });
    });
    $(".new-signed-btn").click(function () {
        Toast("已领取");
    });
    $(".btn").click(function () {
        $.ajax({
            type:'get',
            url:BASE_URl + "user/sign/daily",
            success:function (data) {
                if(data.success){
                    var html = "";
                    if(data.data.gift == 0){
                        html = `<div class="success-title tc">恭喜你签到成功</div><div class="success-award tc">获得${data.data.coin}书币</div>`;
                    }else{
                        html = `<div class="success-title tc">恭喜你签到成功</div><div class="success-award tc">获得200书币+${data.data.gift}书券</div>`;
                    }
                    $(".success-detail").html(html);
                    $(".next-sign-btn .sign-coin").html(data.data.next+"币");
                    $(".coin-wrapper p").html(data.data.user.coin);
                    $(".gift-wrapper p").html(data.data.user.gift);
                    $(".next-sign-btn").show().siblings(".sign-btn").hide();
                    $(".sign-success-wrapper").show();
                    $(".daily-sign-detail li").eq(dailyCurrent).children().addClass("signed");
                    dailyCurrent = data.data.current;
                }else{
                    Toast("本月已签完");
                }
            }
        });
    });
    $(".sign-success-wrapper").click(function () {
        $(this).hide();
    });
    $(".sign-recode-btn").click(function () {
        location.href = "./signRecord.html?channel="+channel;
    });
});

function signConfig() {
    $.ajax({
        type:'get',
        url:BASE_URl + "config/sign",
        success:function (data) {
            if(data.success){
                $(".coin-wrapper p").html(data.data.coin);
                $(".gift-wrapper p").html(data.data.gift);
                DrawSign(data.data.daily);
                DrawNewSign(data.data.new);
            }
        }
    });
}

function DrawNewSign(data) {
    if(data.today){
        $(".new-sign-btn").hide().siblings(".new-btn").show();
    }
    var list = data.list;
    newCurrent = data.current;
    if(newCurrent == 7){
        $(".new-sign-bg").hide();
        $(".big-bg").hide();
        $(".small-bg").show();
    }
    var html = "";
    for(var i = 0;i < list.length;i ++){
        if(list[i].gift == 0){
            if(newCurrent >= i + 1){
                html += `<li class="inline pr active">
                            <p class="pa award-coin active">${list[i].coin}书币</p>
                         </li>`;
            }else{
                html += `<li class="inline pr">
                            <p class="pa award-coin">${list[i].coin}书币</p>
                         </li>`;
            }

        }else{
            if(newCurrent >= i + 1){
                html += `<li class="inline pr active">
                            <p class="pa award-coin active">${list[i].coin}书币</p>
                            <p class="pa award-gift active"><span class="pa tc">+${list[i].gift}书券</span></p>
                         </li>`;
            }else{
                html += `<li class="inline pr">
                            <p class="pa award-coin">${list[i].coin}书币</p>
                            <p class="pa award-gift"><span class="pa tc">+${list[i].gift}书券</span></p>
                         </li>`;
            }
        }
    }
    $(".award-detail-wrapper").html(html);
}

function DrawSign(data) {
    if(data.today){
        $(".sign-btn").hide().siblings(".btn").show();
    }
    $(".next-sign-btn .sign-coin").html(data.next+"币");
    dailyCurrent = data.current;
    var list = data.list;
    var html = "";
    for(var i = 0;i < list.length;i ++){
        if(list[i].gift == 0){
            if(dailyCurrent >= i + 1){
                html += `<li class="inline pr">
                            <div class="award-bg signed"></div>
                            <div class="award-count pr tc signed">已签</div>
                            <div class="award-order tc signed">${list[i].no}</div>
                            <span class="signed-line pa signed"></span>
                         </li>`;
            }else{
                html += `<li class="inline pr">
                            <div class="award-bg"></div>
                            <div class="award-count pr tc">${list[i].coin}币</div>
                            <div class="award-order tc">${list[i].no}</div>
                            <span class="signed-line pa"></span>
                         </li>`;
            }
        }else{
            if(dailyCurrent >= i + 1){
                html += `<li class="inline pr">
                            <div class="award-bg signed"></div>
                            <div class="award-count pr tc signed">已签</div>
                            <div class="award-order tc signed">${list[i].no}</div>
                            <span class="signed-line pa signed"></span>
                         </li>`;
            }else{
                html += `<li class="inline pr">
                            <div class="award-bg"></div>
                            <div class="award-count pr tc">${list[i].coin}币+${list[i].gift}券</div>
                            <div class="award-order tc">${list[i].no}</div>
                            <span class="signed-line pa"></span>
                         </li>`;
            }
        }
    }
    $(".daily-sign-detail ul").html(html);
}

function showSignRule() {
    $(".sign-rule-wrapper").show();
}

function hideSignRule() {
    $(".sign-rule-wrapper").hide();
}